import { message, Modal } from "antd";
import { find } from "lodash";
//参数配置
const checkConfig = {
	//同意仅退款
	'AgreeOnlyRefund': {
		vertValue: [
			[{ key: "afterType", value: 2 }],
			[{ key: "afterType", value: 1 }],
		],
		noDataMsg: "没有可操作的售后单，请重新勾选"
	},
	//同意买家退货
	'AgreeBuyerRefund': {
		vertValue: [[{ key: "afterType", value: 0 }]],
		noDataMsg: "没有可操作的售后单，请重新勾选"
	},
	//买家已退货，同意退款
	'AgreeRefundReturned': {
		vertValue: [
			[
				{ key: "afterType", value: 0 },
				{ key: "platRefundState", value: 3 },
			],
			[
				{ key: "afterType", value: 0 },
				{ key: "platRefuplatRefundStatendStateList", value: -1 },
			],
		],
		noDataMsg: "没有可操作的售后单，请重新勾选"
	},
	//同意补发/换货
	'AgreeExchange': {
		vertValue: [
			[{ key: "afterType", value: 7 }],
			[{ key: "afterType", value: 3 }],
		],
		noDataMsg: "没有可操作的售后单，请重新勾选"
	},
	//拒绝仅退款
	'RefuseOnlyRefund': {
		vertValue: [
			[{ key: "afterType", value: 0 }],
			[{ key: "afterType", value: 1 }],
			[{ key: "afterType", value: 2 }]
		],
		noDataMsg: "该售后单不支持退款！"
	},
	//拒绝买家退货
	'RefuseBuyerReturn': {
		vertValue: [
			[
				{ key: "afterType", value: 0 },
			]
		],
		noDataMsg: "该售后单不支持退货！"
	},
	//拆包异常，拒绝退款
	'RefuseErrorRefund': {
		vertValue: [
			[
				{ key: "afterType", value: 0 },
				{ key: "platRefundState", value: 3 },
			],
		],
		noDataMsg: '该售后单不支持退款！',
	},
	//拒绝补发/换货
	'RefuseExchange': {
		vertValue: [
			[{ key: "afterType", value: 7 }],
			[{ key: "afterType", value: 3 }],
		],
		noDataMsg: "该售后单不支持退货！"
	},
};

//校验符合条件的售后单数量
export const countOrderSum = (scene, selectKeys, tableList) => {
	const { vertValue } = checkConfig[scene];
	const count = [];
	for (let j in vertValue) {
		count[j] = 0;
		for (let i in selectKeys) {
			const obj = find(tableList, ["id", selectKeys[i]]);
			if (obj) {
				let btn = true;
				for (let k in vertValue[j]) {
					if (obj[vertValue[j][k].key] != vertValue[j][k].value) {
						btn = false;
						break;
					}
				}
				if (btn) {
					count[j] = count[j] + 1;
				}
			}
		}
	}
	if (count.length > 1) {
		return count;
	}
	return count[0];
};

//校验是否存在符合要求的售后单
export const checkMatchResult = (scene, selectKeys, tableList) => {
	const { vertValue, noDataMsg } = checkConfig[scene];
	for (let i in selectKeys) {
		const obj = find(tableList, ["id", selectKeys[i]]);
		if (obj) {
			for (let j in vertValue) {
				let btn = true;
				for (let k in vertValue[j]) {
					if (obj[vertValue[j][k].key] != vertValue[j][k].value) {
						btn = false;
					}
				}
				if (btn) {
					return btn;
				}
			}
		}
	}
	message.info(noDataMsg);
	return false;
};

//校验是否包含淘宝网店并且只有一个淘宝的网店
export const checkTaobaoStore = (selectKeys, tableList) => {
	const storeArr = [];
	const taobaoArr = [];
	let isManyStore = false;
	for (let i in selectKeys) {
		const obj = find(tableList, ["id", selectKeys[i]]);
		if (obj.orderChannel == 8) {
			if (storeArr.length > 0) {
				isManyStore = true;
				break;
			} else {
				if (!taobaoArr.includes(obj.ecStoreId)) {
					taobaoArr.push(obj.ecStoreId);
					if (taobaoArr.length > 1) {
						isManyStore = true;
						break;
					}
				}
			}
		} else {
			if (!storeArr.includes(obj.ecStoreId)) {
				storeArr.push(obj.ecStoreId);
			}
		}
	}
	if (isManyStore) {
		Modal.info({
			content: '淘宝网店只能单个网店处理退款，请重新勾选'
		})
	}
	return isManyStore;
};



//校验是否存在淘宝售后单
export const checkHasTaobao = (selectKeys, tableList) => {
	for (let i in selectKeys) {
		const obj = find(tableList, ["id", selectKeys[i]]);
		//淘宝和天猫支持弹框
		if (obj && obj.orderChannel == 8 || obj.orderChannel == 18) {
			return true;
		}
	}
	return false;
};

//校验是否是多店铺
export const isManyStore = (selectKeys, tableList) => {
	let storeId = undefined;
	for (let i in selectKeys) {
		const obj = find(tableList, ["id", selectKeys[i]]);
		if (obj && obj.ecStoreId) {
			if(!storeId) {
				storeId = obj.ecStoreId;
			} else {
				if(storeId != obj.ecStoreId) {
					return true;
				}
			}
		}
	}
	return false;
};